package com.yami.shop.api.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.CopyrightWorks;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserCopyright;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.CopyrightWorksService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.UserCopyrightService;
import com.yami.shop.service.UserService;
import com.yami.shop.user.common.service.UserGasInfoService;
import com.yami.shop.user.common.service.UserGasLogService;
import com.yami.shop.webservice.common.model.*;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @ClassName:
 * @Description: 版权登记
 * @Author:
 * @Date: 2
 * @History 修改描述
 */
@RestController
@RequestMapping(value = "/p/copyright")
public class CopyrightApiController {
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserCopyrightService userCopyrightService;
	@Autowired
	private CopyrightWorksService copyrightWorksService;
	@Autowired
	private ShopConfig shopConfig;
	@Autowired
	private UserService userService;
	@Autowired
	private UserGasLogService userGasLogService;
	@Autowired
	private UserGasInfoService userGasInfoService;

	@Autowired
	private ShopDetailService shopDetailService;
	/**
	 * 商户登记存证注册
	 * @param
	 * @return
	 */
	@PostMapping("/foreign")
	public ServerResponseEntity foreign(@RequestBody UserCopyrightDto userCopyrightDto) {
		String userId = SecurityUtils.getUser().getUserId();
		User userByUserId = userService.getUserByUserId(userId);
		if(StrUtil.isEmpty(userByUserId.getRealName())){
			throw new YamiShopBindException("用户未实名");
		}
		UserCopyright userCopyright = userCopyrightService.getOne(new LambdaQueryWrapper<UserCopyright>().eq(UserCopyright::getUserId, userId));
		if(!ObjectUtil.isNull(userCopyright) && "1".equals(userCopyright.getStatus())){
			throw new YamiShopBindException("审核通过用户信息不能修改");
		}
		if(ObjectUtil.isNull(userCopyright)) userCopyright = new UserCopyright();
		userCopyright.setUserId(userId);
		userCopyright.setStatus("0");
		userCopyright.setIdNo(userByUserId.getIdNumber());
		userCopyright.setOwnerName(userByUserId.getRealName());
		userCopyright.setIdImg(userCopyrightDto.getIdImg());
		userCopyright.setIdImgBack(userCopyrightDto.getIdImgBack());
		userCopyright.setCreateTime(new Date());
		userCopyrightService.saveOrUpdate(userCopyright);
		return  ServerResponseEntity.success(userCopyright);
	}
	@GetMapping("/getForeignInfo")
	public ServerResponseEntity foreign() {
		String userId = SecurityUtils.getUser().getUserId();
		UserCopyright userCopyright = userCopyrightService.getOne(new LambdaQueryWrapper<UserCopyright>().eq(UserCopyright::getUserId, userId));
		return  ServerResponseEntity.success(userCopyright);
	}

	@GetMapping("/checkHaveWorksGas")
	public ServerResponseEntity checkHaveWorksGas() {
		String userId = SecurityUtils.getUser().getUserId();
		//特殊业务判断是否
		Integer workGasReduce = shopConfig.getWorksConfig().getWorksGasReduce();
		Boolean b = userGasInfoService.checkHaveGasNumberByUserId(userId, workGasReduce);
		Map<String,Object> res = new HashMap<>();
		res.put("checkHaveWorksGas",b);
		res.put("workGasReduce",workGasReduce);
		return  ServerResponseEntity.success(res);
	}
	/**
	 * 登记-新增登记作品信息
	 * @param copyrightWorks
	 * @return
	 */
	@PostMapping("/workSaveOrUpdate")
	public ServerResponseEntity workSaveOrUpdate(@RequestBody @Validated CopyrightWorks copyrightWorks) {
		String userId = SecurityUtils.getUser().getUserId();
		UserCopyright userCopyright = userCopyrightService.getOne(new LambdaQueryWrapper<UserCopyright>().eq(UserCopyright::getUserId, userId));
		if(ObjectUtil.isNull(userCopyright)){
			throw new YamiShopBindException("请添加用户信息");
		}
		if(ObjectUtil.isNull(copyrightWorks.getWorksId())){
			String channelCode = shopConfig.getWorksConfig().getChannelCode();
			String chainCode = shopConfig.getWorksConfig().getChainCode();
			ShopDetail shopDetailByShopCode = shopDetailService.getShopDetailByShopCode(channelCode);
			copyrightWorks.setShopId(shopDetailByShopCode.getShopId());
			Integer workGasReduce = shopConfig.getWorksConfig().getWorksGasReduce();
			userGasLogService.reduceGasNumberThansferByUserId(userId,workGasReduce,10,channelCode,chainCode,null);
			copyrightWorks.setCreateTime(new Date());
		}else{
			copyrightWorks.setUpdateTime(new Date());
		}
		copyrightWorks.setStatus("1");//待申请
		copyrightWorks.setUserId(userId);
		copyrightWorksService.saveOrUpdate(copyrightWorks);
		return  ServerResponseEntity.success();
	}

	/**
	 * 登记-取消登记作品信息
	 * @param copyrightWorks
	 * @return
	 */
	@PostMapping("/workCancel")
	public ServerResponseEntity workCancel(@RequestBody CopyrightWorks copyrightWorks) {
		if(ObjectUtil.isNull(copyrightWorks.getWorksId())){
			throw new YamiShopBindException("参数不能为空");
		}
		String userId = SecurityUtils.getUser().getUserId();
		copyrightWorks.setStatus("5");
		copyrightWorks.setUpdateTime(new Date());
		copyrightWorksService.updateById(copyrightWorks);
		//取消申请退回能量值
		String channelCode = shopConfig.getWorksConfig().getChannelCode();
		String chainCode = shopConfig.getWorksConfig().getChainCode();
		ShopDetail shopDetailByShopCode = shopDetailService.getShopDetailByShopCode(channelCode);
		copyrightWorks.setShopId(shopDetailByShopCode.getShopId());
		Integer workGasReduce = shopConfig.getWorksConfig().getWorksGasReduce();
		userGasLogService.saveGasNumberThansferByUserId(userId,workGasReduce,10,channelCode,chainCode,null);
		return  ServerResponseEntity.success();
	}

	/**
	 * 详情
	 * @param worksId
	 * @return
	 */
	@GetMapping("/workInfoByid")
	public ServerResponseEntity workInfoByid(@RequestParam Long worksId) {
		CopyrightWorks byId = copyrightWorksService.getById(worksId);
		UserCopyright userCopyright = userCopyrightService.getOne(new LambdaQueryWrapper<UserCopyright>().eq(UserCopyright::getUserId, byId.getUserId()));
		byId.setOwnName(userCopyright.getOwnerName());
		byId.setWorksTypeName(getWorksTypeName(byId.getWorksType()));
		byId.setInditeNatureName(getInditeNatureName(byId.getInditeNature()));
		return  ServerResponseEntity.success(byId);
	}

	/**
	 *保证书模板下载
	 * @return
	 */
	@GetMapping("/downWorksPowerGuaranteeTemplate")
	public ServerResponseEntity downWorkSpowerGuaranteeTemplate() {
		return  ServerResponseEntity.success(shopConfig.getWorksConfig().getPowerGuaranteeTemplate());
	}
	private String getWorksTypeName(String worksType){
		//A文字B音乐C 戏剧D 曲艺E 舞蹈F 美术G 摄影H 电影I 视听J 工程设计图、产品设计图 K 地图、示意图L 其他作品 M 口述 N 杂技 O 建筑P 模型
		String worksTypeName = null;
		switch (worksType){
			case "A":worksTypeName = "文字";break;
			case "B":worksTypeName = "音乐";break;
			case "C":worksTypeName = "戏剧";break;
			case "D":worksTypeName = "曲艺";break;
			case "E":worksTypeName = "舞蹈";break;
			case "F":worksTypeName = "美术";break;
			case "G":worksTypeName = "摄影";break;
			case "H":worksTypeName = "电影";break;
			case "I":worksTypeName = "视听";break;
			case "J":worksTypeName = "工程设计图、产品设计图";break;
			case "K":worksTypeName = "地图、示意图";break;
			case "L":worksTypeName = "其他作品";break;
			case "M":worksTypeName = "口述";break;
			case "N":worksTypeName = "杂技";break;
			case "O":worksTypeName = "建筑";break;
			case "P":worksTypeName = "模型";break;
		}
		return worksTypeName;
	}
	private String getInditeNatureName(String inditeNature){
		//A文字B音乐C 戏剧D 曲艺E 舞蹈F 美术G 摄影H 电影I 视听J 工程设计图、产品设计图 K 地图、示意图L 其他作品 M 口述 N 杂技 O 建筑P 模型
		String inditeNatureName = null;
		switch (inditeNature){
			case "1":inditeNatureName= "原创";break;
			case "2":inditeNatureName= "改编";break;
			case "3":inditeNatureName= "翻译";break;
			case "4":inditeNatureName= "汇编";break;
			case "5":inditeNatureName= "注释";break;
			case "6":inditeNatureName= "整理";break;
			case "7":inditeNatureName= "其他";break;
		}
		return inditeNatureName;
	}

	@GetMapping("/page")
	@Operation(summary =  "分页查询", description = "分页查询")
	public ServerResponseEntity<IPage<CopyrightWorks>> getNoticePage(PageParam<CopyrightWorks> page, CopyrightWorks copyrightWorks) {
		String userId = SecurityUtils.getUser().getUserId();
		IPage<CopyrightWorks> noticePage = copyrightWorksService.page(page, new LambdaQueryWrapper<CopyrightWorks>()
				.eq(StrUtil.isNotEmpty(copyrightWorks.getStatus()) , CopyrightWorks::getStatus, copyrightWorks.getStatus())
				.eq(CopyrightWorks::getUserId, userId)
				.eq(StrUtil.isNotEmpty(copyrightWorks.getWorksName()) , CopyrightWorks::getWorksName, copyrightWorks.getWorksName()).orderByDesc(CopyrightWorks::getCreateTime));
		if(ObjectUtil.isNotEmpty(noticePage.getRecords())){
			List<CopyrightWorks> records = noticePage.getRecords();
			records.forEach(s->s.setWorksTypeName(getWorksTypeName(s.getWorksType())));
		}
		return ServerResponseEntity.success(noticePage);
	}
}

